const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        last_result: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        
        // 验证登录
        app.verifyLogin();

        // 显示提示框
        if(undefined !== options.message){
            wx.showToast({
                title: options.message,
                icon: 'none'
            });
        };
    },

    /**
     * 验证申请状态
     */
    verifyApply: function() {
        wx.showToast({
            title: '获取数据...',
            icon: 'loading',
            mask: true,
            duration: 50000
        });
        wx.request({
            url: app.data.service_url + 'staff_apply/verify',
            header: {
                'Client-Token': app.data.client_token
            },
            method: 'GET',
            success: function(response) {
                wx.hideToast();
                if (200 === response.statusCode) {
                    if (0 === response.data.errno) {
                        if ('success' === response.data.data.result_string) {
                            wx.showToast({
                                title: '申请成功'
                            });
                            setTimeout(function() {
                                wx.reLaunch({
                                    url: '/pages/staff_home/index/index',
                                })
                            }, 1500);
                            wx.setStorage({
                                key: 'is_staff',
                                data: true
                            });
                            app.data.is_staff = true;
                        } else {
                            let pages = getCurrentPages();
                            let page = pages[pages.length - 1];
                            page.setData({
                                last_result: response.data.data.result_string
                            });
                        };
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideToast();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });

    }
});